<%@ Page Language="C#" Inherits="Natrue.Web.MVC.AuthViewPage" %>
<%@ Import Namespace="Natrue.Popedom.Menu"%>
<%@ Import Namespace="Natrue.Data"%>
<%@ Import Namespace="NHibernate.Expression"%>
<%@ Import Namespace="System.Xml"%>
<%
    Response.ContentType = "text/xml";
    XmlDocument xmldoc = new XmlDocument();
    XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
    xmldoc.AppendChild(xmldecl);

    XmlNode root = xmldoc.CreateElement("", "Menus", "");

    IActiveRecordHelper arHelper = ProxyContainer.FindLocalProxy<IActiveRecordHelper>();

    IList<MenuNode> roots = arHelper.GetIBOList<MenuNode>(new []{Order.Asc("MenuIndex")}, Expression.Eq("IsRoot", true));
    foreach (MenuNode menuNode in roots)
        AddSubMenus(menuNode.MenuKey, menuNode, xmldoc, root);

    xmldoc.AppendChild(root);
    xmldoc.Save(Response.OutputStream);
 %>
 
 <script runat="server">
     
     private void AddSubMenus(string navPath, MenuNode parentMenu, XmlDocument xmlDoc, XmlNode parentNode)
     {
         XmlNode thisNode = xmlDoc.CreateElement("", "Menu", "");
         XmlAttribute att = xmlDoc.CreateAttribute("Caption");
         att.Value = parentMenu.Caption;
         thisNode.Attributes.Append(att);
         XmlAttribute att2 = xmlDoc.CreateAttribute("NavPath");
         att2.Value = navPath;
         thisNode.Attributes.Append(att2);

         ISystemMenu sysMenu = ProxyContainer.FindLocalProxy<ISystemMenu>();
         IList<IMenu> subMenus = sysMenu.GetChildsMenus(parentMenu.PrimaryKey);

         XmlNode subNodes = xmlDoc.CreateElement("", "SubMenus", "");
         XmlNode cmdNodes = xmlDoc.CreateElement("", "CommandMenus", "");
         XmlNode buttons = xmlDoc.CreateElement("", "Buttons", "");
         XmlNode standPages = xmlDoc.CreateElement("", "StandPages", "");

         foreach (IMenu list in subMenus)
         {
             if (list.GetType() == typeof (MenuNode))
                 AddSubMenus(string.Format("{0}/{1}", navPath, list.MenuKey), (MenuNode) list, xmlDoc, subNodes);
             else if (list.GetType() == typeof (MenuCommand))
                 AddCommandMenus(string.Format("{0}/{1}", navPath, list.MenuKey), (MenuCommand) list, xmlDoc, cmdNodes);
         }

         IList<CommandButton> btns = sysMenu.GetChildCommandButton(parentMenu.PrimaryKey);
         if (btns.Count > 0)
             AddButtons(navPath, xmlDoc, buttons, btns);

         IList<StandPage> pages = sysMenu.GetChildStandPages(parentMenu.PrimaryKey);
         if (pages.Count > 0)
             AddStandPage(navPath, xmlDoc, standPages, pages);

         thisNode.AppendChild(subNodes);
         thisNode.AppendChild(cmdNodes);
         thisNode.AppendChild(buttons);
         thisNode.AppendChild(standPages);

         parentNode.AppendChild(thisNode);
     }

     private void AddCommandMenus(string navPath, MenuCommand parentMenu, XmlDocument xmlDoc, XmlNode parentNode)
     {
         XmlNode thisNode = xmlDoc.CreateElement("", "CommandMenu", "");
         XmlAttribute att = xmlDoc.CreateAttribute("Caption");
         att.Value = parentMenu.Caption;
         thisNode.Attributes.Append(att);
         XmlAttribute att2 = xmlDoc.CreateAttribute("NavPath");
         att2.Value = navPath;
         thisNode.Attributes.Append(att2);

         XmlNode buttons = xmlDoc.CreateElement("", "Buttons", "");
         XmlNode standPages = xmlDoc.CreateElement("", "StandPages", "");
         
         ISystemMenu sysMenu = ProxyContainer.FindLocalProxy<ISystemMenu>();
         IList<CommandButton> btns = sysMenu.GetChildCommandButton(parentMenu.PrimaryKey);
         if (btns.Count > 0)
             AddButtons(navPath, xmlDoc, buttons, btns);

         IList<StandPage> pages = sysMenu.GetChildStandPages(parentMenu.PrimaryKey);
         if (pages.Count > 0)
             AddStandPage(navPath, xmlDoc, standPages, pages);

         thisNode.AppendChild(buttons);
         thisNode.AppendChild(standPages);
         parentNode.AppendChild(thisNode);
     }

    private void AddButtons(string navPath, XmlDocument xml, XmlNode buttons, IList<CommandButton> dataSource)
    {
        foreach (CommandButton button in dataSource)
        {
            XmlNode node = xml.CreateElement("", "Button", "");
            XmlAttribute att = xml.CreateAttribute("Caption");
            att.Value = button.ButtonName;
            node.Attributes.Append(att);
            XmlAttribute att2 = xml.CreateAttribute("NavPath");
            att2.Value = String.Format("{0}/{1}", navPath, button.MenuKey);
            node.Attributes.Append(att2);
            buttons.AppendChild(node);
        }
    }

    private void AddStandPage(string navPath, XmlDocument xml, XmlNode standPages, IList<StandPage> dataSource)
    {
         ISystemMenu sysMenu = ProxyContainer.FindLocalProxy<ISystemMenu>();
         foreach (StandPage standPage in dataSource)
         {
             XmlNode node = xml.CreateElement("", "StandPage", "");
             XmlAttribute att = xml.CreateAttribute("Caption");
             att.Value = standPage.PageName;
             node.Attributes.Append(att);
             XmlAttribute att1 = xml.CreateAttribute("NavPath");
             att1.Value = String.Format("{0}/{1}", navPath, standPage.MenuKey);
             node.Attributes.Append(att1);

             XmlNode btns = xml.CreateElement("", "Buttons", "");
             IList<CommandButton> btns1 = sysMenu.GetChildCommandButton(standPage.PrimaryKey);
             if (btns1.Count > 0)
                 AddButtons(att1.Value, xml, btns, btns1);
             node.AppendChild(btns);
             standPages.AppendChild(node);     
         }
    }

    </script>